//
//  ViewController.m
//  05_scrollView
//
//  Created by 402030900@qq.com on 16/7/20.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import "ViewController.h"
// 宏就是系统编译时,代码的替换(预编译指令)
// 宏定义 命名两种方式
// 1. 全大写   2. k + 驼峰命名
#define kScreenBounds [UIScreen mainScreen].bounds
#define WIDTH  kScreenBounds.size.width
#define HEIGHT  kScreenBounds.size.height
#define kScreenWidth [kScreenBounds.size.width
#define kScreenHeight kScreenBounds.size.height

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:kScreenBounds];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor blackColor];
    UIButton * button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [button setFrame:CGRectMake(0, 0, WIDTH - 20 , 100)];
    button.backgroundColor = [UIColor redColor];
    [scrollView addSubview:button];
    // 1.contentSize 默认:CGSizeZero 可以滚动的范围
    scrollView.contentSize = CGSizeMake(WIDTH * 4, 1000);
    // 2.contentOffset 默认:CGPointZero 偏移量,滚动的位置
    scrollView.contentOffset = CGPointMake(0, 500);
    NSLog(@"%@",NSStringFromCGRect(scrollView.frame));
    NSLog(@"%@",NSStringFromCGRect(scrollView.bounds));
    // 问题:改变contentOffset什么变了?
    // 答案: scrollView 的 bounds变了
    // 不设置contentOffset.contentSize 也能改变
    // 3.contentInset
    // 添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 40);
    // 4. directionalLockEnabled 锁定垂直或水平滚动 (45°角,锁会失效)
    scrollView.directionalLockEnabled =YES;
    // 5.bounces 回弹效果
    // 边缘弹动动画关闭 NO
    scrollView.bounces = NO;
    // 6.alwaysBounceVertical 在没有垂直方向的滚动范围时.保证垂直方向的回弹效果存在
    scrollView.alwaysBounceHorizontal = YES;// 水平方向回弹效果存在
    scrollView.alwaysBounceVertical = YES;// 垂直方向回弹效果存在
    // 7.pagingEnabled 按页滚动
    scrollView.pagingEnabled = YES;
    // 8.scrollEnabled 能否滚动
    scrollView.scrollEnabled = YES;
    // 9.是否显示垂直/水平滚动条,默认YES
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.showsVerticalScrollIndicator = YES;
    // 10.滚动条的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    // 11.indicatorStyle 滚动条颜色
    //    UIScrollViewIndicatorStyleDefault,
    //    UIScrollViewIndicatorStyleBlack,
    //    UIScrollViewIndicatorStyleWhite
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite ;
    // 12.相关用户操作
//    scrollView.tracking 是否开始拖动
//    scrollView.dragging 是否正在拖动
//    scrollView.decelerating 是否正在减速
    // 13.双击状态栏 回到顶部位置 默认YES
    scrollView.scrollsToTop = YES;
    // 14.代理
    scrollView.delegate = self;
    // 15. 键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    // 测试
    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 300, WIDTH - 20, 200)];
    [scrollView addSubview:textField];
    textField.backgroundColor = [UIColor whiteColor];
    // 16. 缩放
    // 如果想进行视图的缩放,必须实现viewForZoomingInScrollView
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
    
    

    // Do any additional setup after loading the view, typically from a nib.
}
// 与其想要缩放的子视图绑定,scrollView的contentSize会跟随这个子视图变化
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [scrollView.subviews firstObject];
}
// 滚动视图滚动就会执行
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // tracking 用户touch触摸
    if (scrollView.isTracking) {
        NSLog(@"正在拖动滚动");
    }
    // Dragging 用户开始滑动
    if (scrollView.isDragging) {
        NSLog(@"用户开始滑动");
    }
    // decelerating 用户触摸结束
    if ( scrollView.isDecelerating) {
        NSLog(@"用户触摸结束");
    }
   }
// 滚动视图将要开始拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"开始拖拽");
}
// 滚动视图已经结束拖拽
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"已经结束拖拽");
}
// 将要开始减速
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"开始减速");
}
// 已经结束减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"已经结束减速");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
